tongfusoft.role = function() {
}

tongfusoft.role.prototype = {
		//角色列表
		roleTable : function() {

			var table = new tongfusoft.table();
			var columns = [
					{
						"data" : "roleId"
					},
					{
						"data" : "roleName"
					},
					{
						"data" : "loginName"
					},
					{
						"data" : "lastUpdDate"
					},
					{
						"data" : "roleId",
						"render" : function(data, type, full, meta) {
							var delbut = '<button roleId="'
									+ data
									+ '" onclick="new tongfusoft.role().roleDel(this)" type="button" data-toggle="modal" data-target="#delrole" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'
							var editbut = '<button roleId="'
									+ data
									+ '" onclick="new tongfusoft.role().roleEdit(this)" type="button" data-toggle="modal" data-target="#delroleClass" class="detBtn btn btn-default btn-sm "><em class="fa fa-edit"></em></button>'

							return editbut + delbut;
						}

					} ]

			table.dataTable('roleTable', 'manager/role/query', columns, null);

			$('#roleTable').DataTable().on(
					'select',
					function(e, dt, type, indexes) {
						$("#roleId").val($('#roleTable').DataTable().rows(indexes).data()[0].roleId);
						$("#roleTableTitle").text(
								"角色名："+$('#roleTable').DataTable().rows(indexes).data()[0].roleName);
						
						$('#userTable').DataTable().clear();
						$('#userTable').DataTable().ajax.url(
								common.getContextPath('manager/userList/query?roleId=' + $("#roleId").val())).load();
					});

		},
		
		//对应的用户
		userTable : function() {

			var table = new tongfusoft.table();
			var columns = [
					{
						"data" : "ruId"
					},
					{
						"data" : "userName"
					},
					{
						"data" : "ruId",
						"render" : function(data, type, full, meta) {
							var del = '<button ruId="'
									+ data
									+ '" onclick="new tongfusoft.role().userDel(this)" type="button" data-toggle="modal" data-target="#delgroupClass" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>'

							return del;
						}

					} ]

			table.dataTable('userTable', 'manager/userList/query?roleId=' + $("#roleId").val(), columns, null);
			
			$('#userTable').DataTable().on(
					'select',
					function(e, dt, type, indexes) {
						$("#ruId").val($('#userTable').DataTable().rows(indexes).data()[0].rpId);
					});
		},

		//新增角色
		addBtnRole : function() {
			$("#addBtnRole").click(function() {
				$("#roleModelTitle").text('新增');
				common.clean("roleForm");
				$('#roleModel').modal('show');
				$("#oper").val("add");

			})

		},
	    //删除角色
		roleDel: function(obj){
			var flag = false;
			common.post('manager/isRoleHasUsers', {
				"roleId" : $(obj).attr("roleId")
			}, function(data) {
				if (data.state == 'success' && data.data != 0) {
					flag = true;
				}
			});
			if(flag==true){
				toastr["error"]('该角色已绑定用户，无法删除!');
			}else{
				
				swal({
					title : "是否确认?",
					text : "",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "删除",
					cancelButtonText : "取消",
					closeOnConfirm : true
				}, function() {
					common.ajaxSubmitOnSuccess({
						"roleId" : $(obj).attr("roleId")
					}, 'manager/role/del', function(data) {

						if (data.state == 'success') {

							$('#roleTable').DataTable().ajax.reload();
							$('#userTable').DataTable().ajax.reload();
						}else{
							toastr["error"]('删除失败！');
						}

					}, "del")
				});

			}
			
		},
		// 角色修改
		roleEdit : function(obj) {

			common.post('manager/role/detail', {
				"roleId" : $(obj).attr("roleId")
			}, function(data) {
				$("#roleModelTitle").text('修改');
				$('#roleModel').modal('show');
				$("#oper").val("edit");
				if (data.state == 'success') {

					$('#roleName').val(data.data.roleName);
					$('#roleId').val(data.data.roleId);
				}else{
					toastr["error"]('保存失败！');
				}

			});

		},
		 //提交submit
		roleSubmit :function(){
			$.validator.setDefaults({
				submitHandler : function() {
					var initData = common.getData("roleForm");
					var url = 'manager/role/add';

					if ($("#oper").val() == 'edit') {

						url = 'manager/role/edit';
						initData['roleId'] = $("#roleId").val();
					}
					common.ajaxSubmitOnSuccess(initData, url, function(data) {
						if (data.state == 'success') {

							$('#roleModel').modal('hide');
							$('#roleTable').DataTable().ajax.reload();

							$('#uesrTable').DataTable().ajax.url(
									common.getContextPath('manager/userList/query?roleId=' + $("#roleId").val())).load();

						} else if (data.code == 'repeat_error') {

							toastr["error"]('角色名不能重复!');
						}else{
							toastr["error"]('保存失败！');
						}

					}, $("#oper").val())
				}
			});
			$("#roleForm").validate();

			$("#submitBtn").click(function() {

				$("#roleForm").submit();
			})
		},
		
		userDel :function(obj){
			swal({
				title : "确认删除?",
				text : "",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "删除",
				cancelButtonText : "取消",
				closeOnConfirm : true
			}, function() {
				common.ajaxSubmitOnSuccess({
					"ruId" : $(obj).attr("ruId")
				}, 'manager/roleHasUsers/del', function(data) {

					if (data.state == 'success') {
						$('#roleTable').DataTable().ajax.reload();
						$('#userTable').DataTable().ajax.reload();
						toastr["success"]('删除成功！');
					}else{
						toastr["error"]('删除失败！');
					}

				})
			});
		},
		
		// 用户列表
		userAddTable : function() {
			$("#addUserBut").click(function() {
				var roleId = $("#roleId").val();
				if(roleId ==null || roleId ==""){
					toastr["error"]('请先选择角色!');
				}else{

					common.post('manager/userUnSelect/query', {
					"roleId" : roleId
				}, function(data) {
					
					$("#userData").html('');
					var userData='';
					if (data.state == 'success') {
						
						$.each(data.data, function(index, item) {
							var userHtml =        ' <li style="clear:both;list-style-type: none">';
							userHtml = userHtml + ' <div class="col-md-1" style="width:50px;">';
							userHtml = userHtml +       '<div class="checkbox c-checkbox">';	
							userHtml = userHtml +        '<label><input type="checkbox" name="choiceUser" value="'+item.userId+'"/> <span class="fa fa-check"></span></label>'
							userHtml = userHtml +       '</div>';	
							userHtml = userHtml + '</div>';		
							userHtml = userHtml + '<div class="col-md-6">';	
							userHtml = userHtml +    '<div style="padding-top:7px;">'+item.loginName +'</div>';	
							userHtml = userHtml + '</div>';	
							userHtml = userHtml + '</li>';	
							
							userData =userData+userHtml;
						});
					}
					$("#userData").html(userData);
				});
					
					$('#myModal').modal('show');
				}

			})

		},
		//全选
		AllCheck : function(){
			var allUserCheck = document.getElementsByName("choiceUser");
			var checkUserAll = document.getElementById("checkUserAll");
			$("#checkUserAll").click(function() {
				if(checkUserAll.checked == true) {
					for(var i = 0; i < allUserCheck.length; i++){
						allUserCheck[i].checked = true;
					}
				 } else {
					 for(var i = 0; i < allUserCheck.length; i++){
						 allUserCheck[i].checked = false;
						}
				 }
			});

		},
		
		//提交角色和用户权限操作
		userSubmit : function(){
			$.validator.setDefaults({
				submitHandler : function() {
					var initData = common.getData("userForm");
					initData['roleId'] = $("#roleId").val();
					var url = 'manager/userHasRoles/add';

					common.ajaxSubmitOnSuccess(initData, url, function(data) {

						if (data.state == 'success') {
							$('#myModal').modal('hide');
							//new tongfusoft.role2().userAddTable();
							//new tongfusoft.role2().userTable();
							$('#userTable').DataTable().ajax.reload();
							//new tongfusoft.role2().roleTable();
							toastr["success"]('保存成功!');
						} else {

							toastr["error"]('添加角色权限失败!');
						}

					})
				}
			});
			$("#userForm").validate();

			$("#submitUser").click(function() {
				    var userId = "";
					var temp = "";
					var choiceUser = document.getElementsByName("choiceUser");
					for ( var i = 0; i < choiceUser.length; i++) {
						if (choiceUser[i].checked) {
						   temp = choiceUser[i].value;
						   userId = userId + "," +temp;
						}
					}
					$("#userIdList").val(userId)
					
				$("#userForm").submit();

			})
		},
		
}

$(function() {

	var role = new tongfusoft.role();
	// 角色表表格
	role.roleTable();
	//权限表
	role.userTable();
	//角色新增
	role.addBtnRole();
	//
	role.userAddTable();
	//提交新增
	role.roleSubmit();
	//提交用户与角色权限
	role.userSubmit();

	role.AllCheck();
})